/**
 * Created by chenwenhao on 2017/1/13.
 */
import {Injectable} from '@angular/core';
import {Response} from "@angular/http";
import {BehaviorSubject, Observable} from "rxjs";

import {HttpService} from "@core/http/http.service";
import * as API from "@api/index";
import {SeoService} from "@core/seo/seo.service";
import {AppPlayTurn, ArticleCate, ArticleVO, FeedCommentRespVO, FeedRespVO} from "@interface/index";
// import {AppPlayTurn, ArticleVO, FeedCommentRespVO, FeedRespVO} from "@interface/interface/interface.model";
// import {ArticleCate} from "@interface/community/community.model";

@Injectable()
export class NewsService {
  public _banners: BehaviorSubject<AppPlayTurn[]> = new BehaviorSubject<AppPlayTurn[]>([]);
  public _cates: BehaviorSubject<ArticleCate> = new BehaviorSubject<ArticleCate>(new ArticleCate());
  public _articles: BehaviorSubject<ArticleVO[]> = new BehaviorSubject<ArticleVO[]>([]);
  public _news: BehaviorSubject<ArticleVO[]> = new BehaviorSubject<ArticleVO[]>([]);
  public _feeds: BehaviorSubject<FeedRespVO[]> = new BehaviorSubject<FeedRespVO[]>([]);
  public _feed: BehaviorSubject<FeedRespVO[]> = new BehaviorSubject<FeedRespVO[]>([]);
  public _feedComment: BehaviorSubject<FeedCommentRespVO[]> = new BehaviorSubject<FeedCommentRespVO[]>([]);
  public dataStore = {
    banners: [] = [],
    cates: {} = {},
    articles: [] = [],
    news: [] = [],
    feeds: [] = [],
    feed: [] = [],
    feedComment: [] = [],
  };
  public infoStore = {};

  constructor(
    public httpService: HttpService,
    private _seoService: SeoService
  ) {
    this.infoStore['index'] = 0;
    this.infoStore['postion'] = 0;
  }


  get news() {
    return this._news.asObservable();
  }

  // 获取资讯cate分类下的文章描述的文章详细内容
  GetCateArticleInfo(articleId: ArticleVO) {
    const url = API.INFO_CATE_ARTICLE_URL + `?articleId=${articleId}`;
    return this.httpService.get(url).map((res: Response) => res.json());
  }

  // 获取资讯cate分类下的文章描述的文章详细内容下的推荐文章
  GetCateArticleListRecommend(cateId: ArticleVO, articleId: ArticleVO) {
    const url = API.INFO_CATE_ARTICLE_LIST_RECOMMEND_URL + `?categoryId=${cateId }&articleId=${articleId}`;
    return this.httpService.get(url).map((res: Response) => res.json());
  }

  // 获取资讯cate分类下的文章描述的文章详细内容下的评论
  GetCateArticleListComment(articleId: number, page: number = 1, rows: number = 3) {
    const url = API.INFO_CATE_ARTICLE_LIST_COMMENT_URL + `?articleId=${articleId}&page=${page}&rows=${rows}`;
    return this.httpService.get(url).map((res: Response) => res.json());
  }


  /**
   *
   * @description 获取帖子相关数据
   *
   */
  get feeds() {
    return this._feeds.asObservable();
  }

  GetFeedForGuest(page: number = 1, rows: number = 10) {
    const url = API.FEED_GUEST_URL;
    this.httpService
      .get(`${url}?page=${page}&rows=${rows}`)
      .map((res: Response) => res.json())
      .subscribe((res) => {
        this.dataStore.feeds = [...res.data.list];
        this._feeds.next(this.dataStore.feeds);
      });
  }

  get feedArticle() {
    return this._feed.asObservable();
  }

  GetFeedArticle(feedId: FeedRespVO) {
    const url = API.FEED_ARTICLE_URL + `?feedId=${feedId}`;
    this.httpService
      .get(url)
      .flatMap((resp: Response) => {
        const res = resp.json();
        if (res && res.result === '0') {
          this.dataStore.feed = [res.data];
          this._feed.next(this.dataStore.feed);
          const circleName = res.data.circleName;
          if (!circleName) {
            res.data.circleName = "";
          }
          this._seoService.setTKD(
            `${res.data.title}_体育比赛资讯`,
            `${res.data.title}_${res.data.circleName}_${res.data.labels[0].labelName}`,
            `求苗体育聚集各类体育产业IP，包含但不限于体育赛事、体育培训、体育旅游、体育赞助、体育用品等各大板块，
      实现多种资源的聚合与互通, 赛事运营管理、合作机构对接、消费数据统计及线上推广等多服务轻松一键搞定的健康运动服务的互联网平台`
          );
        }
        return this.GetFeedArticleComment(feedId, 1);
      }).subscribe(res => {

      if (res && res.result === '0') {
        this.dataStore.feedComment = [...res.data.list];
        this._feedComment.next(this.dataStore.feedComment);
      }
    });
  }

  get feedComment() {
    return this._feedComment.asObservable();
  }

  GetFeedArticleComment(feedId: FeedRespVO, page: number, rows: number = 3) {
    const url = API.FEED_ARTICLE_COMMENT_URL + `?feedId=${feedId}&page=${page}&rows=${rows}`;
    return this.httpService.get(url).map((res: Response) => res.json());
  }

  GetFeedDigg(feedId: number, userId: string, operation: boolean = true) {
    const data = {
      feedId: feedId,
      userId: userId
    };

    if (operation) {
      const url = API.FEED_ARTICLE_DIGG_URL;
      return this.httpService.postUrlencode(url, data).map((res) => res.json());

    } else {
      const url = API.FEED_ARTICLE_DIGG_URL + `?feedId=${feedId}&userId=${userId}`;
      return this.httpService.delete(url).map((res: Response) => res.json());
    }
  }

  GetFeedArticleCommentDigg(commentId: number, userId: string, operation: boolean = true) {
    const data = {
      userId: userId,
      commentId: commentId
    };
    if (operation) {
      const url = API.FEED_ARTICLE_COMMENT_DIGG_URL;
      return this.httpService.postUrlencode(url, data).map((res: Response) => res.json());
    } else {
      const url = API.FEED_ARTICLE_COMMENT_DIGG_URL + `?userId=${userId}&commentId=${commentId}`;
      return this.httpService.delete(url).map((res: Response) => res.json());
    }
  }

  getPhotoNews(articleId: number, rows: number) {
    const url = API.PHOTO_NEWS_URL + `?articleId=${articleId}&rows=${rows}`;
    return this.httpService.get(url).map((res: Response) => res.json());
  }

  getRecommendPhotoNews(cateId: number, articleId: number, rows: number = 6) {
    const url = API.RECOMMEND_PHOTO_NEWS_URL + `?cateId=${cateId}&articleId=${articleId}&rows=${rows}`;
    return this.httpService.get(url).map((res: Response) => res.json());
  }

}
